package com.ch.sys.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ch.fastbuilder.common.GenerationType;
import com.ch.fastbuilder.model.BuilderModel;
import com.ch.fastbuilder.model.Model;

public class Function extends Model {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public static String TABLE = "function";
	public static String ID = "id";
	public static String P_ID = "p_id";
	public static String TITLE = "title";
	public static String URL = "url";
	public static String ICON = "icon";
	public static String ORDER = "order";
	public static String CREATED = "created";

	public Function() {
		super.setPrimaryKey(ID);
		super.setTableName(TABLE);
		//super.setGenerationType(GenerationType.UUID);
		super.setGenerationType(GenerationType.IDENTITY);
		super.setColumns(ID,P_ID,TITLE,URL,ICON,ORDER,CREATED);
	}
	
	
	public static List<Function> find(Long roleId, Long pId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("role_id", roleId);
		params.put("p_id", pId);
		
		BuilderModel builder = Model.Select("f.id, f.p_id, f.title, f.url, f.icon,  f.created");
		builder.from("function f");
		builder.innerJoin("role_function rf on f.id = rf.function_id");
		builder.where("rf.role_id = :role_id AND f.p_id = :p_id", params);
		
		return builder.list(Function.class);
	}
}
